﻿@using Microsoft.JSInterop;
@using System.Resources;
@using System.IO;
@inject IJSRuntime JS

<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

<WpfBlazor.PageComponets.FirstPage></WpfBlazor.PageComponets.FirstPage>

<h2>Read a file</h2>
<p>@dataResourceText</p>

<h2>Show an image</h2>
<p><img alt="1991 Jeep YJ" src="/jeep-yj.png" /></p>
<p>
    <em>Jeep</em> and <em>Jeep YJ</em> are registered trademarks of
    <a href="https://www.stellantis.com">FCA US LLC (Stellantis NV)</a>.
</p>
<p>
    <button @onclick="TriggerPrompt">Trigger browser window prompt</button>
</p>

<p>@result222</p>
<p>
    <button @onclick="TriggerPrompt222">222 Trigger browser window prompt</button>
</p>

@code {
    private IJSObjectReference module;

    private string dataResourceText = "Loading resource ...";
    private string result = "";
    private string result222 = "222";

    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }

    private async Task TriggerPrompt()
    {
        result = await Prompt("Provide some text");
    }
    private async Task TriggerPrompt222()
    {
        var mmd = await JS.InvokeAsync<IJSObjectReference>("import", "./Counter.razor.js");
        result222 = await module.InvokeAsync<string>("showPrompt", "default message");
    }

    public async ValueTask<string> Prompt(string message) =>
         module is not null ? await module.InvokeAsync<string>("showPrompt", message) : "";

    private async Task<string> ReadData()
    {
        using var reader = new StreamReader("wwwroot/data.txt");
        return await reader.ReadToEndAsync();
    }

    protected override void OnInitialized()
    {
        var resources = new ResourceManager(typeof(WpfBlazor.Properties.Resources));
        dataResourceText = resources.GetString("Data") ?? "'Data' not found.";
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            module = await JS.InvokeAsync<IJSObjectReference>("import", "./js/testjs.js");
        }
    }

    void Test()
    {
        Version v1 = new Version(0, 0, 0, 1);
        var vvv = new List<Version>() { v1 };


    }
}